package com.bachelor.community.service.impl;

import cn.hutool.core.convert.Convert;
import com.bachelor.community.mapper.AnswerMapper;
import com.bachelor.community.mapper.CommentMapper;
import com.bachelor.community.mapper.QuestionMapper;
import com.bachelor.community.mapper.UserMapper;
import com.bachelor.community.model.entity.Answer;
import com.bachelor.community.model.entity.Comment;
import com.bachelor.community.model.entity.Question;
import com.bachelor.community.model.entity.User;
import com.bachelor.community.model.vo.CommentVO;
import com.bachelor.community.service.intf.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private AnswerMapper answerMapper;
    @Resource
    private QuestionMapper questionMapper;

    @Override
    public List<CommentVO> listComments(Integer answerId) {
        List<CommentVO> commentVOList = commentMapper.selectCommentList(answerId);
        commentVOList.forEach(commentVO -> {
            Answer answer = answerMapper.selectById(commentVO.getAnswerId());
            Question question = questionMapper.selectById(answer.getQuestionId());
            commentVO.setQuestionerId(question.getCreator());
            if (commentVO.getReplyTo() != null) {
                User replyTo = userMapper.selectById(Convert.toInt(commentVO.getReplyTo()));
                commentVO.setReplyTo(replyTo.getUsername());
            }
        });
        return commentVOList;
    }
}
